// test
// function api_robot_test(params, callback) {
//     axios_request({
//         url: '/robot/test',
//         method: 'get',
//         data: params
//     }, function (res) {
//         callback(res)
//     })
// }

// 登录
function api_loginCheck(data, callback) {
    axios_request({
        url: '/loginCheck',
        method: 'post',
        data: JSON.stringify(data),
        contentType: 'application/json',
    }, function (res) {
        callback(res)
    })
}

// 退出登录
function api_logout(data, callback) {
    axios_request({
        url: '/logout',
        method: 'post',
        data: JSON.stringify(data),
        contentType: 'application/json',
    }, function (res) {
        callback(res)
    })
}

// 创建机器人
function api_robot_create(data, callback) {
    axios_request({
        url: '/robot/create',
        method: 'post',
        data: JSON.stringify(data),
        contentType: 'application/json',
    }, function (res) {
        callback(res)
    })
}

// 更新机器人
function api_robot_update(data, callback) {
    axios_request({
        url: '/robot/update',
        method: 'post',
        data: JSON.stringify(data),
        contentType: 'application/json',
    }, function (res) {
        callback(res)
    })
}

// 重命名机器人
function api_robot_updateName(data, callback) {
    axios_request({
        url: '/robot/updateName',
        method: 'post',
        data: JSON.stringify(data),
        contentType: 'application/json',
    }, function (res) {
        callback(res)
    })
}

// 更新状态
function api_robot_updatePublishStatus(data, callback) {
    axios_request({
        url: '/robot/updatePublishStatus',
        method: 'post',
        data: JSON.stringify(data),
        contentType: 'application/json',
    }, function (res) {
        callback(res)
    })
}

// 更新助手主题
function api_robot_updateAgentShowTitle(data, callback) {
    axios_request({
        url: '/robot/updateAgentShowTitle',
        method: 'post',
        data: JSON.stringify(data),
        contentType: 'application/json',
    }, function (res) {
        callback(res)
    })
}

// 删除机器人
function api_robot_delete(data, callback) {
    axios_request({
        url: '/robot/delete',
        method: 'post',
        data: data,
    }, function (res) {
        callback(res)
    })
}

// 复制机器人
function api_robot_copy(data, callback) {
    axios_request({
        url: '/robot/copy',
        method: 'post',
        data: data,
    }, function (res) {
        callback(res)
    })
}

// 锁定
function api_robot_lock(data, callback) {
    axios_request({
        url: '/robot/lock',
        method: 'post',
        data: data,
    }, function (res) {
        callback(res)
    })
}

// 解锁
function api_robot_unlock(data, callback) {
    axios_request({
        url: '/robot/unlock',
        method: 'post',
        data: data,
    }, function (res) {
        callback(res)
    })
}


// AIchat 发送消息
async function api_chat_stream_send(data, callback) {
    var agentCode = data.agentCode;
    var url = '/chat/stream/' + agentCode
    fetch_request(url, data, function (res) {
        callback(res)
    })
}


// 获取 OSS 上传所需的临时凭证
function api_file_getUploadCredentials(params, callback) {
    axios_request({
        url: '/file/getUploadCredentials',
        method: 'get',
        data: params
    }, function (res) {
        callback(res)
    })
}

// oss 直传图片/文件
function api_oss_upload_file(data, process, callback) {
    const host = data.get('host');
    axios_request({
        url: host,
        method: 'post',
        data: data,
        contentType: 'multipart/form-data', // 重要：上传文件必须设置
        onUploadProgress: function (progressEvent) {
            const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
            // console.log(`上传进度: ${percentCompleted}%`);
            // 这里可以更新进度条UI
            process(percentCompleted)
        }
    }, function (res) {
        // console.log('res->', res)
        callback(res)
    })
}


// 会话列表获取
function api_robot_session_list(data, callback) {
    axios_request({
        url: '/session/record/list',
        method: 'get',
        data: data,
    }, function (res) {
        callback(res)
    })
}

// 会话详情列表获取
function api_get_chat_messages(data, callback) {
    axios_request({
        url: '/session/record/detail',
        method: 'get',
        data: data,
    }, function (res) {
        callback(res)
    })
}

// 获取NLS一句话语音识别TOKEN信息
function api_nls_get_token(data, callback) {
    axios_request({
        url: '/chat/voice/nls-token',
        method: 'get',
        data: data,
    })
}

// 插件列表
function api_plugin_list(params, callback) {
    var url = '/chat/plugin/list';
    if (params.agentId) {
        url += '?agentId=' + params.agentId;
    }

    fetch(url, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        }
    })
    .then(response => response.json())
    .then(data => {
        if (callback) callback(data);
    })
    .catch(error => {
        console.error('获取插件列表失败:', error);
        if (callback) callback({code: 500, message: '网络错误'});
    });
}

/**
 * 获取智能体已绑定的插件列表
 */
function api_agent_plugins(agentId, callback) {
    fetch('/chat/plugin/agent/' + agentId, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        }
    })
    .then(response => response.json())
    .then(data => {
        if (callback) callback(data);
    })
    .catch(error => {
        console.error('获取智能体插件列表失败:', error);
        if (callback) callback({code: 500, message: '网络错误'});
    });
}

/**
 * 绑定插件到智能体
 */
function api_plugin_bind(params, callback) {
    fetch('/chat/plugin/bind', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(params)
    })
    .then(response => response.json())
    .then(data => {
        if (callback) callback(data);
    })
    .catch(error => {
        console.error('插件绑定失败:', error);
        if (callback) callback({code: 500, message: '网络错误'});
    });
}

/**
 * 解绑插件
 */
function api_plugin_unbind(agentId, pluginId, callback) {
    fetch('/chat/plugin/unbind/' + agentId + '/' + pluginId, {
        method: 'DELETE',
        headers: {
            'Content-Type': 'application/json',
        }
    })
    .then(response => response.json())
    .then(data => {
        if (callback) callback(data);
    })
    .catch(error => {
        console.error('插件解绑失败:', error);
        if (callback) callback({code: 500, message: '网络错误'});
    });
}

/**
 * 更新插件绑定配置
 */
function api_plugin_update(params, callback) {
    fetch('/chat/plugin/update', {
        method: 'PUT',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(params)
    })
    .then(response => response.json())
    .then(data => {
        if (callback) callback(data);
    })
    .catch(error => {
        console.error('插件配置更新失败:', error);
        if (callback) callback({code: 500, message: '网络错误'});
    });
}
